//
//  FRMainViewController.m
//  Live
//
//  Created by 徐芙蓉 on 2017/7/25.
//  Copyright © 2017年 Afra. All rights reserved.
//

#import "FRMainViewController.h"
#import "FRMainTopView.h"

@interface FRMainViewController ()<UIScrollViewDelegate>
@property (weak, nonatomic) IBOutlet UIScrollView *contentScrollView;
@property (nonatomic, strong) NSArray * datalist;
@property (nonatomic, strong) FRMainTopView *topView;
@end

@implementation FRMainViewController
-(FRMainTopView *)topView
{
    if (!_topView) {
        _topView = [[FRMainTopView alloc]initWithFrame:CGRectMake(0, 0, 200, 50) titleNames:self.datalist];
//        __weak typeof (self) weakSelf = self;
        @weakify(self);//使用YYKIT
        _topView.block = ^(NSInteger tag){
            @strongify(self);
            CGPoint point = CGPointMake(SCREEN_WIDTH * tag, self.contentScrollView.contentOffset.y);
            [self.contentScrollView setContentOffset:point animated:YES];
        };
    }
    return _topView;
}
-(NSArray *)datalist
{
    if (!_datalist) {
        _datalist = @[@"关注",@"热门",@"附近"];
    }
    return _datalist;
}
- (void)viewDidLoad {
    [super viewDidLoad];

    
    [self initUI];
    
    
}
-(void)initUI
{
    //添加左右按钮
    [self setupNav];
    
    //添加子视图控制器
    [self setupChildViewControllers];
    
}
-(void)setupChildViewControllers
{
    NSArray *vcNames = @[@"FRFocusViewController",@"FRHotViewController",@"FRNearViewController"];
    
    for (NSInteger i = 0; i < vcNames.count; i++) {
        NSString *vcName = vcNames[i];
        UIViewController *vc = [[NSClassFromString(vcName) alloc]init];
        vc.title = self.datalist[i];
        //当执行addChildViewController时,不会执行该VC的Viewdidload
        [self addChildViewController:vc];
    }
    //将子控制器的View,加到mainVC的scrollView上
    
    //设置scrollView的contentsize
    self.contentScrollView.contentSize = CGSizeMake(SCREEN_WIDTH * self.datalist.count, 0);
    //进入主控制器加载第二个页面
    self.contentScrollView.contentOffset = CGPointMake(SCREEN_WIDTH, 0);
    self.contentScrollView.delegate = self;
    [self scrollViewDidEndScrollingAnimation:self.contentScrollView];
}
-(void)setupNav
{
    self.navigationItem.titleView = self.topView;
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"global_search"] style:UIBarButtonItemStyleDone target:nil action:nil];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"title_button_more"] style:UIBarButtonItemStyleDone target:nil action:nil];

}

//动画结束时调用
-(void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView
{
    CGFloat width = SCREEN_WIDTH;
    CGFloat height = SCREEN_HEIGHT;
    
    CGFloat offset = scrollView.contentOffset.x;
    //获取索引值
    NSInteger idx = offset / width;

    //索引值联动topview
    [self.topView scrolling:idx];
    
    //根据索引值返回VC的引用
    UIViewController *vc = self.childViewControllers[idx];
    
    //判断当前VC是否执行过viewdidload
    if ([vc isViewLoaded]) return;
    
    //设置子控制器View的大小
    vc.view.frame = CGRectMake(offset, 0, width, height);
    
    //将子控制器的View加入scrollView上
    [scrollView addSubview:vc.view];

}

//减速结束时调用加载子控件View的方法
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    [self scrollViewDidEndScrollingAnimation:scrollView];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
